<?php
header('content-type:text/html charset:utf-8');
date_default_timezone_set('PRC');

require_once 'inc/mysql.php';

use OSS\OssClient;

$dir_base ="houseimg/".date('Ym',time()).'/';
$index = 0;
$output = '';
$up_config = $db->query_first('select * from hjmall_upload_config where store_id = 0 and is_delete = 0 limit 1');
$upload_type = $up_config['storage_type'];

foreach($_FILES as $file)
{
    $upload_file_name = 'upload_file' . $index;
    $filename = $_FILES[$upload_file_name]['name'];
    $name_arr = explode('.',$filename);

    $file_ext = strtolower(trim(array_pop($name_arr)));

    for($i = 0;$i<10;$i++)
    {
        $saveName =substr('abcdefhjkmnpqrstuvwxy345678ABCDEFGHJKLMNPQRSTUVWXY0oz2il1g9',(mt_rand(0,58)),1);
    }
    $filename = $saveName.date('YmdHis',time()) . '.' . $file_ext;
    $rEFileTypes = "/^\.(jpg|jpeg|gif|png){1}$/i";
    if(!preg_match($rEFileTypes, strrchr($filename, '.'))){
        $output .= "";
    }else{
        if($upload_type == 'qiniu'){
            $path = saveToQiniu($up_config['qiniu'],$file['tmp_name'],$dir_base,$filename);
        }elseif($upload_type == 'aliyun' ){
            $path = saveToAliyun($up_config['aliyun'],$file['tmp_name'],$dir_base,$filename);
        }elseif($upload_type == 'qcloud'){
            $output .= '';
        }else
        {
            $path = saveToLocal($file['tmp_name'],$dir_base,$filename);
        }
		
		if($path['code'] != 1){
			$output .= $path['data']['url'] . ',';
		}else {
			$output .= "";
		}
    }
}
echo $output;die;



function image_auto($srcimage,$desimage,$deswidth,$desheight,$quality=90){
    if(is_file($srcimage)) {
        list($srcwidth,$srcheight,$srctype)=getimagesize($srcimage);
        switch($srctype) {
            case 1:$im=imagecreatefromgif($srcimage);break;//gif
            case 2:$im=imagecreatefromjpeg($srcimage);break;//jpg
            case 3:$im=imagecreatefrompng($srcimage);break;//png
        }
        if($srcwidth/$srcheight>=$deswidth/$desheight){
            if($srcwidth>$deswidth){$deswidth=$deswidth;$desheight=($srcheight*$deswidth)/$srcwidth;}
            else{$deswidth=$srcwidth;$desheight=$srcheight;}
        }
        else{
            if($srcheight>$desheight){$desheight=$desheight;$deswidth=($srcwidth*$desheight)/$srcheight;}
            else{$deswidth=$srcwidth;$desheight=$srcheight;}
        }
        $ni=imagecreatetruecolor($deswidth,$desheight);
        imagecopyresampled($ni,$im,0,0,0,0,$deswidth,$desheight,$srcwidth,$srcheight);
        imagejpeg($ni,$desimage,$quality);//$quality=100:设置图片品质为100
        imagedestroy($ni);imagedestroy($im);
    }
}
//image_auto($dir_base.$filename,$dir_base.$filename,1024,4000);


function saveToLocal($file_path, $saveDir, $saveName, $type = 0)//$file['tmp_name'],$dir_base,$filename
{
	if(!file_exists($saveDir . $saveName)){
		$res = move_uploaded_file($file_path, $saveDir . $saveName);
		if(!$res){
			return ['code'=>1,'msg'=>'error'];
		}
	}
	if($type == 0){
		
	}
	return [
		'code'	=>	0,
		'msg'	=>	'success',
		'data'	=>	['url'=>'http://' . $_SERVER['SERVER_NAME'] . '/admin/' . $saveDir . $saveName]
	];
}

function saveToAliyun($config,$file_path, $saveDir, $saveName, $type = 0)
{
    require_once './extensions/aliyun-oss-php-sdk-2.2.4/autoload.php';
    $config = json_decode($config, true);
    if($config['CNAME'] == 0){
        $ossClient = new OssClient($config['access_key'],$config['secret_key'],$config['domain']);
    }else{
        $ossClient = new OssClient($config['access_key'],$config['secret_key'],$config['domain'],true);
    }
    $res = $ossClient->uploadFile($config['bucket'],$saveDir.$saveName,$file_path);
    if (file_exists($file_path)) {
        unlink($file_path);
    }
    if($config['CNAME'] == 0){
        $arr = explode('//',$config['domain']);
        $url = $arr[0].'//'.$config['bucket'].'.'.$arr[1] . '/' . $saveDir . $saveName;
    }else{
        $url = $config['domain'].'/'.$saveDir.$saveName;
    }
    if ($type == 0) {
        $url .= ($config['style_api'] ? '?' . $config['style_api'] : '');
    }
    if (!empty($res)) {
        return [
            'code' => 0,
            'msg' => 'success',
            'data' => [
                'url' => $url,
            ],
        ];
    }else{
		return ['code'=>1,'msg'=>'error'];
	}
}
function saveToQiniu($config,$file_path, $saveDir, $saveName, $type = 0)
{
    require_once './extensions/qiniu/autoload.php';

    $config = json_decode($config,true);
    $accessKey = $config['access_key'];//getenv('QINIU_ACCESS_KEY');
    $secretKey = $config['secret_key'];//getenv('QINIU_SECRET_KEY');
    $bucket = $config['bucket'];//getenv('QINIU_TEST_BUCKET');
    $auth = new \Qiniu\Auth($accessKey, $secretKey);
    $token = $auth->uploadToken($bucket);
    $uploadMgr = new \Qiniu\Storage\UploadManager();

    // 上传文件到七牛
    list($ret, $err) = $uploadMgr->putFile($token, $saveDir . $saveName, $file_path);
    if (file_exists($file_path)) {
        unlink($file_path);
    }
    $url = $config['domain'] . '/' . $saveDir . $saveName;
    if ($err !== null) {
        return [
            'code' => 1,
        ];
    } else {
        if ($type == 0) {
            $url .= ($config['style_api'] ? '?' . $config['style_api'] : '');
        }
        return [
            'code' => 0,
            'msg' => 'success',
            'data' => [
                'url' => $url,
            ],
        ];
    }
}

?>
